package com.longrise.android.byjk.advertisement.InterstitialAD;

import android.app.Activity;
import android.app.Dialog;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;


import com.longrise.android.byjk.advertisement.admanager.ADCacheHelper;
import com.longrise.android.byjk.advertisement.admanager.ADIndexCallback;
import com.longrise.android.byjk.advertisement.admanager.ADIndexbean;
import com.longrise.android.byjk.advertisement.admanager.ADManagerHttpHelper;
import com.longrise.android.byjk.advertisement.utils.ADFileUtil;
import com.longrise.android.byjk.advertisement.utils.ADHttpUtil;
import com.longrise.android.byjk.advertisement.utils.ADHttpUtilCallback;
import com.longrise.android.byjk.advertisement.utils.MD5Utils;

import java.io.File;


/**
 * Created by callxkj on 2016/12/14.
 * 插屏广告
 */

public class InterstitialAD {

    private final Activity mActivity;
    private final InterstitialDialog mDialog;
    private InterstitialADListener mListener;
    private boolean flag; //是否能显示dialog

    public InterstitialAD(Activity activity) {
        this.mActivity = activity;
        mDialog = new InterstitialDialog(mActivity);
    }


    public Dialog show() {
        if (flag) {
            mDialog.show();
            return mDialog;
        }
        return null;
    }

    public void setInterstitialADListener(InterstitialADListener listener) {
        this.mListener = listener;
        getIndexbean();
    }

    /**
     * 每次通过请求获取此用户是否有广告
     */
    private void getIndexbean() {
        ADManagerHttpHelper.getInstance().initAdindex(new ADIndexCallback() {
            @Override
            public void onSuccess() {
                parseIndexbean();
            }

            @Override
            public void onFail() {
               if(mListener!=null){
                   mListener.onFail();
               }
            }
        });

    }

    /**
     * 解析bean 通过bean来判断此账号是不是有广告
     */
    private void parseIndexbean() {
        ADIndexbean aDindexBean = ADCacheHelper.getInstance().getADindexBean();
        if (aDindexBean != null) {
            getImage(aDindexBean);
        }
    }

    private void getImage(ADIndexbean indexbean) {
        if (indexbean == null) {
            return;
        }
        final String url = indexbean.getAdssrc();
        final String adshref = indexbean.getAdshref();
        ADHttpUtil adHttpUtil = new ADHttpUtil();
        adHttpUtil.getFilePath(ADHttpUtil.INTERSTITIAL_DIALOG, url, new ADHttpUtilCallback() {
            @Override
            public void onSuccess(String filePath) {
                String name;
                try {
                    name = MD5Utils.getVal_UTF8(url);
                    File file = new File(ADFileUtil.getInterstitialFolderPath(), name);
                    Bitmap bitmap = BitmapFactory.decodeFile(file.getAbsolutePath());
                    mDialog.setImage(bitmap, adshref);
                    flag = true;
                    if (mListener != null) {
                        mListener.onSuccess();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    if(mListener!=null){
                        mListener.onFail();
                    }
                }
            }

            @Override
            public void onFail(String errmsg) {
               if(mListener!=null){
                   mListener.onFail();
               }
            }
        });
    }

}
